package com.tbeasy.financial.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tbeasy.financial.R;
import com.tbeasy.financial.data.TransactionGroup;
import com.tbeasy.financial.data.TransactionItem;
import com.tbeasy.financial.util.Util;

/**
 * 
 * @author Andy
 * 
 */
public class TransactionAdapter extends BaseExpandableListAdapter {

	private LayoutInflater inflater;

	private ArrayList<TransactionGroup> items;

	public TransactionAdapter(Context context) {
		inflater = LayoutInflater.from(context);
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return items.get(groupPosition).getItems().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		ChildViewHolder childHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.transaction_list_item, null);
			childHolder = new ChildViewHolder(convertView);
			convertView.setTag(childHolder);
		} else {
			childHolder = (ChildViewHolder) convertView.getTag();
		}

		childHolder.setValue(items.get(groupPosition).getItems().get(childPosition));

		return convertView;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupViewHolder groupHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.transaction_list_group_item, null);
			groupHolder = new GroupViewHolder(convertView);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupViewHolder) convertView.getTag();
		}

		groupHolder.setValue(items.get(groupPosition));

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		ArrayList<TransactionItem> list = items.get(groupPosition).getItems();
		return list == null ? 0 : list.size();
	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return items.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return items == null ? 0 : items.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	@Override
	public boolean isEmpty() {
		return items == null || items.isEmpty();
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		items.get(groupPosition).setExpanded(false);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		items.get(groupPosition).setExpanded(true);
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	private final class GroupViewHolder {

		private TextView monthView;
		private TextView incomeView;
		private TextView costView;
		private ImageView expanderView;

		public GroupViewHolder(View view) {
			monthView = (TextView) view.findViewById(R.id.trans_list_group_month);
			incomeView = (TextView) view.findViewById(R.id.trans_list_group_income);
			costView = (TextView) view.findViewById(R.id.trans_list_group_cost);
			expanderView = (ImageView) view.findViewById(R.id.trans_list_group_expander_img);
		}

		private void setValue(TransactionGroup groupData) {
			monthView.setText(groupData.getMonth());
			incomeView.setText("￥" + groupData.getIncome());
			costView.setText("￥" + groupData.getCost());
			if (groupData.isExpanded()) {
				expanderView.setImageResource(R.drawable.widget_expander_ic_maximized);
			} else {
				expanderView.setImageResource(R.drawable.widget_expander_ic_minimized);
			}
		}
	}

	private final class ChildViewHolder {

		private TextView categoryNameView;
		private TextView amountView;
		private TextView timeView;

		public ChildViewHolder(View view) {
			categoryNameView = (TextView) view.findViewById(R.id.trans_list_item_category_name);
			amountView = (TextView) view.findViewById(R.id.trans_list_item_amount);
			timeView = (TextView) view.findViewById(R.id.trans_list_item_time);
		}

		private void setValue(TransactionItem data) {
			if (data.getCategoryId() == 1) {
				categoryNameView.setText("支出");
			} else {
				categoryNameView.setText("收入");
			}
			amountView.setText("￥" + data.getAmount());
			timeView.setText(Util.formatDate(data.getAddTime()));
		}
	}

	public void setItems(ArrayList<TransactionGroup> items) {
		this.items = items;
	}

}
